package com.stars.empoly.utils

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.layout.QMUILinearLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.stars.empoly.R
import com.stars.empoly.inte.ChoiceData

/**
 * 去支付
 *Created by shihuiyun
 *on 2020/9/21
 */
object  PopuPayUtils {

    fun show(context: Context,type:Int?,money:String?, view: View, choiceData:ChoiceData){

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.popu_pay, null)

        val ll:LinearLayout=layout.findViewById(R.id.ll)
        val tvMoney:TextView=layout.findViewById(R.id.tv_money)
        val llWx:LinearLayout=layout.findViewById(R.id.ll_wx)
        val imgWx:ImageView=layout.findViewById(R.id.img_wx)
        val llZhi:LinearLayout=layout.findViewById(R.id.ll_zhi)
        val imgZhi:ImageView=layout.findViewById(R.id.img_zhi)
        val tvClose:TextView=layout.findViewById(R.id.tv_close)

        val llYu:LinearLayout=layout.findViewById(R.id.ll_yu)
        val imgYu:ImageView=layout.findViewById(R.id.img_yu)
        val llSubmit: QMUILinearLayout =layout.findViewById(R.id.ll_submit)




        tvMoney.text=money
        var payType=0//1 余额 2微信 3支付宝

        when(type){
            1->{
                payType=1
                llYu.setBackgroundResource(R.drawable.bg_pay_on)
                llWx.setBackgroundResource(R.drawable.bg_pay_off)
                llZhi.setBackgroundResource(R.drawable.bg_pay_off)
                imgYu.visibility=View.VISIBLE
                imgWx.visibility=View.GONE
                imgZhi.visibility=View.GONE
            }
            2->{
                payType=2
                llYu.setBackgroundResource(R.drawable.bg_pay_off)
                llWx.setBackgroundResource(R.drawable.bg_pay_on)
                llZhi.setBackgroundResource(R.drawable.bg_pay_off)
                imgYu.visibility=View.GONE
                imgWx.visibility=View.VISIBLE
                imgZhi.visibility=View.GONE
            }
            3->{
                payType=3
                llYu.setBackgroundResource(R.drawable.bg_pay_off)
                llWx.setBackgroundResource(R.drawable.bg_pay_off)
                llZhi.setBackgroundResource(R.drawable.bg_pay_on)
                imgYu.visibility=View.GONE
                imgWx.visibility=View.GONE
                imgZhi.visibility=View.VISIBLE
            }
        }





        llYu.onClick {
            payType=1
            llYu.setBackgroundResource(R.drawable.bg_pay_on)
            llWx.setBackgroundResource(R.drawable.bg_pay_off)
            llZhi.setBackgroundResource(R.drawable.bg_pay_off)
            imgYu.visibility=View.VISIBLE
            imgWx.visibility=View.GONE
            imgZhi.visibility=View.GONE

        }

        llWx.onClick {
            payType=2
            llYu.setBackgroundResource(R.drawable.bg_pay_off)
            llWx.setBackgroundResource(R.drawable.bg_pay_on)
            llZhi.setBackgroundResource(R.drawable.bg_pay_off)
            imgYu.visibility=View.GONE
            imgWx.visibility=View.VISIBLE
            imgZhi.visibility=View.GONE
        }

        llZhi.onClick {
            payType=3
            llYu.setBackgroundResource(R.drawable.bg_pay_off)
            llWx.setBackgroundResource(R.drawable.bg_pay_off)
            llZhi.setBackgroundResource(R.drawable.bg_pay_on)
            imgYu.visibility=View.GONE
            imgWx.visibility=View.GONE
            imgZhi.visibility=View.VISIBLE
        }


        llSubmit.onClick {
            if(payType==0){
                Toast.makeText(context,"请选择支付方式！",Toast.LENGTH_SHORT).show()
                return@onClick
            }

            if(payType==2){
                Toast.makeText(context,"微信支付暂时不可用！",Toast.LENGTH_SHORT).show()
                return@onClick
            }
            if(null!=choiceData){
                choiceData.onRestuse(payType.toString())

            }
            mNormalPopup.dismiss()
        }


        tvClose.onClick {
            mNormalPopup.dismiss()
        }
        ll.onClick {

        }

        builder.release();

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                height)
        frameLayout.addView(layout, lp)


        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
        mNormalPopup.show(view)


    }




}
